home *** CD-ROM | disk | FTP | other *** search
- Path: druid.borland.com!usenet
- From: pete@borland.com (Pete Becker)
- Newsgroups: comp.lang.c
- Subject: Re: C beginner needs your help ASAP
- Date: 19 Feb 1996 20:46:44 GMT
- Organization: Borland International
- Distribution: world
- Message-ID: <4ganjk$hr1@druid.borland.com>
- References: <4g862f$p0b@risky.ecs.umass.edu> <4g8ahd$p8b@spectator.cris.com>
- NNTP-Posting-Host: pbecker.borland.com
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=ISO-8859-1
- X-Newsreader: WinVN 0.99.5
-
- In article <4g8ahd$p8b@spectator.cris.com>, aubrey@concentric.net says...
- >
- >In article <4g862f$p0b@risky.ecs.umass.edu>, sebag@ecs.umass.edu says...
- >>
- >>I am a new C programmmer who desperately needs help.
- >>I have been digging in the manuals for a way to do this but have still
- >>come out empty handed. Here is the problem: I want to open files in a while
- >>loop with different filenames. data0,data1,data2,data3, .. data1000 , ...
- >>I need to increment an integer each time around then convert it to a string
- >>and then somehow use strcat to combine the "data" with the integer string.
- >>After that use fopen(filename, "a");
- >>
- >>If you know how to do this, please email me. It would take you 2 minutes but
- >>it is taking me two days so far. Code would be extremly useful.
- >>Thanks, sebag@ecs.umass.edu
- >
- >This is how I would do it. I tested it and it seems to do what you want. Of
- >course, I am no expert, in fact I was called "ignorant and idiot" by Mister
- Dan
- >Pop, the self-appointed comp.lang.c.police, for helping someone earlier, so I
- >guess I am not worthy of posting here, but somehow I feel a little better
- after
- >helping someone, instead of attacking and belittling them because I somehow
- see
- >myself as superior when I don't really know anything about them.
- >
- >#include <stdio.h>
- >
- >main()
- >{
- > char buf[3],filename[9];
- > int i;
- >
- > for(i=0; i<10; i++)
- > {
- > sprintf(buf,"%d",i);
- > strcpy( filename,"data");
- > strcat( filename, buf );
- > strcat( filename, ".ext");
- > printf( "%s\n", filename);
- > }
- >
- >}
- >
-
- This is basically right, but it has has one significant problem:
- "data1000.ext" requires 13 chars, not 9. Once that is fixed, the code can be
- made much more succinct:
-
- #include <stdio.h>
-
- int main()
- {
- int i;
- char filename[13];
- for( i = 0; i < 10; i++ )
- {
- sprintf( filename, "data%d.ext", i );
- printf( "%s\n", filename );
- }
- return 0;
- }
-
-